package com.asdc.demo.exchage;

import com.asdc.demo.utils.RabbitMqUtils;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

/**
 * @ClassName ReceiveLogs01
 * @Author bsy
 * @Description topic 交换机
 * @Date 2022/3/13 20:34
 * @Version 1.0
 */
public class ReceiveLogsTopic02 {
    private static final String EXCHANE_NAME = "topic_logs";
    private static final String QUEUE_NAME = "Q2";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        //交换机类型
        channel.exchangeDeclare(EXCHANE_NAME, BuiltinExchangeType.TOPIC);
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        //绑定
        channel.queueBind(QUEUE_NAME, EXCHANE_NAME, "*.*.rabbit");
        channel.queueBind(QUEUE_NAME, EXCHANE_NAME, "lazy.#");
        System.out.println("等待接收消息	");
        DeliverCallback deliverCallback = (consumerTag, delivery) ->
        {
            String message = new String(delivery.getBody(), "UTF-8");
            System.out.println("ReceiveLogsTopic01 控制台打印接收到的消息:" + delivery.getEnvelope().getRoutingKey() + "---" + message);
        };
        channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> {
        });
    }
}
